package com.gree.scmservice.service.impl;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.SysDictData;
import com.gree.common.entity.SysDictType;
import com.gree.common.entity.TablePageData;
import com.gree.common.utils.DictUtils;
import com.gree.common.utils.StringUtils;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.SysDictDataDao;
import com.gree.scmservice.dao.SysDictTypeDao;
import com.gree.scmservice.service.SysDictTypeService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by 180686 on 2021/3/24 8:48
 */

@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {

    @Autowired
    private SysDictDataDao sysDictDataDao;

    @Autowired
    private SysDictTypeDao sysDictTypeDao;

    @Override
    public TablePageData<SysDictType> selectDictTypeList(SysDictType dictType) {
        TablePageData<SysDictType> sysDictTypeTablePageData = sysDictTypeDao.selectSysDictTypeList(dictType);
        return sysDictTypeTablePageData;
    }

    @Override
    public List<SysDictType> selectDictTypeAll() {
        return sysDictTypeDao.querySysDictTypeAll();
    }

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            return dictDatas;
        }
        dictDatas = sysDictDataDao.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return sysDictTypeDao.fetchSysDictTypeByDictId(dictId);
    }

    @Override
    public SysDictType selectDictTypeByType(String dictType) {
        return sysDictTypeDao.fetchSysDictTypeByDictName(dictType);
    }

    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds)
        {
            SysDictType dictType = selectDictTypeById(dictId);
            if (sysDictDataDao.countDictDataByType(dictType.getDict_type()) > 0)
            {
                throw new RuntimeException(String.format("%1$s已分配,不能删除", dictType.getDict_name()));
            }
        }
        int count = sysDictTypeDao.deleteSysDictTypeByDictIds(dictIds);
        if (count > 0)
        {
            DictUtils.clearDictCache();
        }
        return count;
    }

    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }

    @Override
    public int insertDictType(SysDictType sysDictType) {
        sysDictType.setCreate_by(SecurityUtils.getUser().getUsername());
        sysDictType.setCreate_time(new Date());
        SysDictType result = sysDictTypeDao.insertSysDictType(sysDictType);
        if (result != null)
        {
            DictUtils.clearDictCache();
        }
        return 1;
    }

    @Override
    public int updateDictType(SysDictType sysDictType) {
        SysDictType oldDict = sysDictTypeDao.fetchSysDictTypeByDictId(sysDictType.getDict_id());
        sysDictDataDao.updateDictDataType(oldDict.getDict_type(), sysDictType.getDict_type());
        sysDictType.setCreate_by(oldDict.getCreate_by());
        sysDictType.setCreate_time(oldDict.getCreate_time());
        sysDictType.setUpdate_by(SecurityUtils.getUser().getUsername());
        sysDictType.setUpdate_time(new Date());
        int result = sysDictTypeDao.updateSysDictType(sysDictType);
        if (result > 0)
        {
            DictUtils.clearDictCache();
        }
        return result;
    }

    @Override
    public boolean checkDictTypeUnique(SysDictType sysDictType) {
        Long dictId = StringUtils.isNull(sysDictType.getDict_id()) ? -1L : sysDictType.getDict_id();
        SysDictType sysDictType1 = sysDictTypeDao.fetchSysDictTypeByDictType(sysDictType.getDict_type());
        if (StringUtils.isNotNull(sysDictType1) && sysDictType1.getDict_id().longValue() != dictId.longValue())
        {
            return true;
        }
        return false;
    }
}
